package graphicInterface;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListModel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

import recipie.core.Ingredient;
import recipie.core.IngredientCollection;
import recipie.core.MesureUnit;

public class subGUI_addIngr2Recipies extends JPanel {

    JPanel listIngredient;
    JPanel ingredient;
    
    JPanel searchIngredient;
    JTextField searchField;
    JButton search;
    JButton add;
    
    JList lI;
    JScrollPane ingr;
    
    IngredientCollection aI;
    ArrayList lst;
    
    public subGUI_addIngr2Recipies(IngredientCollection a){
        aI = a;
        setList();
        setIngr();
        setLayout(new BorderLayout());
        this.add(listIngredient,BorderLayout.EAST);
        this.add(ingredient,BorderLayout.CENTER);
    }
    
    public void setIngr(){
        ingredient = new JPanel(new BorderLayout(5,5));
        JButton c = new JButton("c");
        ingredient.add(c);
    }
    
    public void setList(){
        
        listIngredient = new JPanel(new BorderLayout(5,5));
        listIngredient.setBorder(new EmptyBorder(5, 5, 5, 5));
        listIngredient.setBorder(new TitledBorder("Select Ingredient"));
        
        searchIngredient = new JPanel(new BorderLayout(5,5));
        searchIngredient.setSize(100, 700);
        searchField = new JTextField(20);
        searchIngredient.add(searchField,BorderLayout.WEST);
        searchField.setLayout(new BorderLayout());
        search = new JButton("s");
        searchIngredient.add(search,BorderLayout.CENTER);
        add = new JButton("Add");
        searchIngredient.add(add,BorderLayout.EAST);
        listIngredient.add(searchIngredient,BorderLayout.NORTH);
        /*DEBUG*/
        aI = new IngredientCollection();
        aI.addIngredient(new Ingredient("sale",MesureUnit.pizz));
        aI.addIngredient(new Ingredient("pepe",MesureUnit.pizz));
        aI.addIngredient(new Ingredient("noce moscata",MesureUnit.pizz));
        /*END*/
        lst = new ArrayList();
        for(Ingredient a: aI.getIngredients().values()){
             lst.add(a.getName());
        }
        lI = new JList(lst.toArray());
        lI.setFont(new Font("Year supply of fairy cakes", Font.PLAIN, 11));
        lI.setFixedCellHeight(18);
        
      
      /*  JList lI = new JList();
        for(Ingredient a: aI.getIngredients().values()){
            JLabel l = new JLabel(a.getName());
            lI.add(l);
            }
             */
        lI.setAlignmentX(5);
        
        ingr = new JScrollPane(lI);
        listIngredient.add(ingr,BorderLayout.CENTER);
      
        lI.addMouseListener( new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) {
                    int index = lI.locationToIndex(e.getPoint());    
                    if (index >= 0) {
                        Object o = lI.getModel().getElementAt(index);
                        System.out.println("Double-clicked on: " + o.toString());
                        ListModel modello=(ListModel)lI.getModel();
                        ((Container) modello).remove(lI.getSelectedIndex());
                    }
                }
            }   
        });
        
        
      
    }
    
    
    

}
